<?php
namespace Api\Model;
use Think\Model\RelationModel;

class PhoneModel extends RelationModel
{
    public function checkAndSave($data)
    {
        if(!$data['mac'])
            return false;
        $phone = $this->where(['mac'=>$data['mac']])->find();
        if($phone){
            $this->data($phone)->where(array('id'=>$phone['id']))->save();
            return $phone;
        }else{
            $data = $this->create(checkArr($data));
            $id = $this->add($data);
            $data['id'] = $id;
            return $data;
        }
    }



    protected function _before_insert(&$data, $options)
    {
        parent::_before_insert($data, $options); // TODO: Change the autogenerated stub
        $data['created_at'] = date("Y-m-d H:i:s",time());
    }

    protected function _before_write(&$data)
    {
        parent::_before_write($data); // TODO: Change the autogenerated stub
        $data['updated_at'] = date("Y-m-d H:i:s",time());
    }
    protected function _after_select(&$resultSet, $options)
    {
        parent::_after_select($resultSet, $options); // TODO: Change the autogenerated stub
    }
}
